const express = require('express');
const router = express.Router();
const { Setting } = require('../models');
const { failureResponse, successResponse } = require('../utils/responses');
const { NotFoundError } = require('../utils/errors')
const { setKey, getKey } = require('../utils/redis');

router.get('/', async function(req, res, next) {
  try {
    const cacheKey = 'setting'
    let setting = await getKey(cacheKey)
    if (!setting) {
      setting = await Setting.findOne()
      if (!setting) {
        throw new NotFoundError('未找到系统设置');
      }
      await setKey(cacheKey, setting);
    }
    successResponse(res, '', { setting });
  } catch (error) {
    failureResponse(res, error);
  }
});

module.exports = router;